//
//  BaseViewController.m
//  Jula2
//
//  Created by Captain on 14-3-4.
//  Copyright (c) 2014年 Captain. All rights reserved.
//

#import "BaseViewController.h"
//#import "MobClick.h"

#import "HighLightedControl.h"

#import "IconLabel.h"

#define PAGE_STR @"page"
#define PAGE_SIZE_STR @"limit"
#define TITLE_ICON_SIZE 27
#define TITLE_FONT_SIZE 18

typedef NS_ENUM(NSInteger,ViewType) {
    ViewTypeTableView =0,
    ViewTypeCollection,
    ViewTypeScroll
};
//#import <Frontia/Frontia.h>
@interface BaseViewController ()
{
    NSString *dateUrl;
    NSInteger intDataID;
}
@property (nonatomic) ViewType viewType;

@property (nonatomic, strong) UIScrollView *scroller;

@property(strong,nonatomic)IconLabel* iconLeft,*iconRight;
@property(strong,nonatomic)UIAlmasLabel *lblTitle,*lblRight,*lblLeft;
@property(strong,nonatomic)HighLightedControl *leftItemView,*rightItemView;
@end

@implementation BaseViewController
#pragma mark - Lifecycle
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {

    }
    
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    //[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    /*
     NSString *url=@"http://aaaaa.arzu.me/cnr/index.php?action=1";
     httpRequest=[ASIHTTPRequest requestWithURL:[NSURL URLWithString:url]];
     httpRequest.delegate=self;
     //设置缓存时间
     httpRequest.secondsToCache=24*60*60*7;//一周
     
     //缓存类
     ASIDownloadCache *_cache=[ASIDownloadCache sharedCache];
     
     [httpRequest setCacheStoragePolicy:ASICachePermanentlyCacheStoragePolicy];
     [httpRequest setDownloadCache:_cache];
     [httpRequest setCachePolicy:ASIOnlyLoadIfNotCachedCachePolicy];
     httpRequest.delegate = self;
     [httpRequest startAsynchronous];
     */
    [self setNeedsStatusBarAppearanceUpdate];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.extendedLayoutIncludesOpaqueBars = NO;
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.navigationController.navigationBar.translucent = NO;
    [self.navigationItem setHidesBackButton:YES];
}
-(UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleDefault;
}
-(void) viewDidAppear:(BOOL)animated
{
//    [super viewDidAppear:animated];
//    [MobClick beginLogPageView:[NSString stringWithUTF8String:object_getClassName(self)]];
//    [BaiduMobStat defaultStat];
    
//        [[BaiduMobStat defaultStat] pageviewStartWithName:[NSString stringWithUTF8String:object_getClassName(self)]];
//    FrontiaStatistics* statTracker = [Frontia getStatistics];
//    [statTracker pageviewStartWithName:[NSString stringWithUTF8String:object_getClassName(self)]];
}

-(void) viewDidDisappear:(BOOL)animated
{
//    [MobClick endLogPageView:[NSString stringWithUTF8String:object_getClassName(self)]];
//        [[BaiduMobStat defaultStat] pageviewEndWithName:[NSString stringWithUTF8String:object_getClassName(self)]];
//    FrontiaStatistics* statTracker = [Frontia getStatistics];
//    [statTracker pageviewEndWithName:[NSString stringWithUTF8String:object_getClassName(self)]];
}

#pragma mark - LazyLoad
-(IconLabel *)iconLeft{
    if (!_iconLeft) {
        _iconLeft = [[IconLabel alloc]initWithFrame:CGRectMake(0, 0, TITLE_ICON_SIZE, TITLE_ICON_SIZE)];
        _iconLeft.textColor = [ReaderTools colorWithHexString:@"616161"];
    }
    return _iconLeft;
}
-(IconLabel *)iconRight{
    if (!_iconRight) {
        _iconRight = [[IconLabel alloc]initWithFrame:CGRectMake(0, 0, TITLE_ICON_SIZE, TITLE_ICON_SIZE)];
        _iconRight.textColor = [ReaderTools colorWithHexString:@"616161"];
    }
    return _iconRight;
}
-(UIAlmasLabel *)lblTitle{
    if (!_lblTitle) {
        _lblTitle = [[UIAlmasLabel alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
        _lblTitle.textColor = [ReaderTools colorWithHexString:@"616161"];
        [_lblTitle setTextSize:TITLE_FONT_SIZE];
    }
    return _lblTitle;
}
-(UIAlmasLabel *)lblLeft{
    if (!_lblLeft) {
        _lblLeft = [[UIAlmasLabel alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
        _lblLeft.textColor = [ReaderTools colorWithHexString:@"616161"];
        [_lblLeft setTextSize:TITLE_FONT_SIZE];
    }
    return _lblLeft;
}
-(UIAlmasLabel *)lblRight{
    if (!_lblRight) {
        
        
        _lblRight = [[UIAlmasLabel alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
        _lblRight.textColor = [ReaderTools colorWithHexString:@"616161"];
        [_lblRight setTextSize:TITLE_FONT_SIZE];
    }
    return _lblRight;
}
-(HighLightedControl *)leftItemView{
    if (!_leftItemView) {
        _leftItemView = [[HighLightedControl alloc] initWithFrame:CGRectMake(0, 0, TITLE_ICON_SIZE, TITLE_ICON_SIZE)];
        [_leftItemView addTarget:self action:@selector(clickedLeftControl:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _leftItemView;
}
-(HighLightedControl *)rightItemView{
    if (!_rightItemView) {
        _rightItemView = [[HighLightedControl alloc] initWithFrame:CGRectMake(0, 0, TITLE_ICON_SIZE, TITLE_ICON_SIZE)];
        [_rightItemView addTarget:self action:@selector(clickedRightControl:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _rightItemView;
}
#pragma mark - Custom Accessors
-(void)setHexLeft:(NSString *)hexLeft{
    _hexLeft = hexLeft;
    [self.leftItemView.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [obj removeFromSuperview];
    }];
    [self.leftItemView addSubview:self.iconLeft];
    self.iconLeft.hexText = hexLeft;
    self.tabBarController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.leftItemView];
    if (self.tabBarController!=nil) {
        self.tabBarController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.leftItemView];
    }else{
        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.leftItemView];
    }
    
}
-(void)setHexRight:(NSString *)hexRight{
    _hexRight = hexRight;
    [self.rightItemView.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [obj removeFromSuperview];
    }];

    [self.rightItemView addSubview:self.iconRight];
    self.iconRight.hexText = hexRight;
    if (self.tabBarController!=nil) {
        self.tabBarController.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.rightItemView];
    }else{
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.rightItemView];
    }
    
}
-(void)setStrTitle:(NSString *)strTitle{
    _strTitle = strTitle;
    self.lblTitle.text = strTitle;
    [self.lblTitle sizeToFit];
    
    
    if (self.tabBarController!=nil) {
        self.tabBarController.navigationItem.titleView = self.lblTitle;
    }else{
        self.navigationItem.titleView = self.lblTitle;
    }
    
}

-(void)setStrTitleLeft:(NSString *)strTitleLeft{
    _strTitleLeft = strTitleLeft;
    self.lblLeft.text = _strTitleLeft;
    [self.leftItemView.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [obj removeFromSuperview];
    }];
    [self.leftItemView addSubview:self.lblLeft];
    [self.lblLeft sizeToFit];
    [self.leftItemView sizeToFit];
    
    
    if (self.tabBarController!=nil) {
        self.tabBarController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.leftItemView];
    }else{
        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.leftItemView];
    }
}
-(void)setStrTitleRight:(NSString *)strTitleRight{
    _strTitleRight = strTitleRight;
    self.lblRight.text = strTitleRight;
    [self.rightItemView.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [obj removeFromSuperview];
    }];
    [self.lblRight sizeToFit];
    [self.rightItemView addSubview:self.lblRight];
    [self.rightItemView sizeToFit];
    
    if (self.tabBarController!=nil) {
        self.tabBarController.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.rightItemView];
    }else{
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.rightItemView];
    }
}
-(void)setViewContent:(UIView *)viewContent{
    _viewContent = viewContent;
    if ([_viewContent isKindOfClass:[UITableView class]]) {
        _viewType = ViewTypeTableView;
    }else if([_viewContent isKindOfClass:[UICollectionView class]]){
        _viewType = ViewTypeCollection;
    }else if([_viewContent isKindOfClass:[UIScrollView class]]){
        _viewType = ViewTypeScroll;
    }
    if ([viewContent isKindOfClass:[UIScrollView class]])
        _scroller = (UIScrollView*)viewContent;
//    self.viewWaitingView = [WaitingView getView];
//    [self.viewWaitingView setUpLayoutWithViewController:self sameSizeView:viewContent];
//    
//    self.viewRetryView = [RetryView getView];
    __weak BaseViewController *weakSelf = self;
//    [self.viewRetryView setUpLayoutWithViewController:self sameSizeView:viewContent retry:^{
//        [weakSelf beginGetDate:dateUrl and:0];
//    }];
//    self.viewEmpty = [EmptyView getView];
//    [self.viewEmpty setUpLayoutWithViewController:self sameSizeView:viewContent];
}
//-(void)contentView:(UIView*)cV enableHeader:(BOOL)header enableFooter:(BOOL)footer{
//    self.viewContent=cV;
//    if ([cV isKindOfClass:[UIScrollView class]]) {
//        UIScrollView *sc = (UIScrollView *)cV;
//        if (header) {
//            sc.header = [CommonRefreshHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
//        }
//        if (footer) {
//            sc.footer = [CommonAutoFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
//        }
//    }
//}
-(void)setColorLeft:(UIColor *)colorLeft{
    _colorLeft = colorLeft;
    self.lblLeft.textColor = colorLeft;
}
-(void)setColorRight:(UIColor *)colorRight{
    _colorRight = colorRight;
    self.lblLeft.textColor = colorRight;
}
-(void)setBaseColor:(UIColor *)baseColor{
    _baseColor = baseColor;
    self.lblTitle.textColor = baseColor;
    self.iconLeft.textColor = baseColor;
    self.iconRight.textColor = baseColor;
}
#pragma mark - Internet

///**
// *  创建网络请求获取第一页面的数据
// */
//-(void)beginGetDate:(NSString *)url and:(NSInteger)intID{
//    dateUrl = url;
//    intDataID = intID;
//    _nowPage = 1;
//    if (self.showContentViewWhenFirstPage&&self.nowPage==1) {
//        
//        if (self.scroller!=nil) {
//            [self.scroller.header beginRefreshing];
////            return;
//        }
//    }else if (!self.showContentViewWhenFirstPage&&self.nowPage==1) {
//        [self showWaiting];
//    }
//    if (intDataID != 0) {
//        NSString *strDataID = [NSString stringWithFormat:@"%ld",(long)intDataID];
//        dateUrl = [NSString stringWithFormat:@"%@%@",dateUrl,strDataID];
//    }
//    [self getDate]; 
//}

bool isSecondRequest;
//-(void)getDate{
//    if (self.showContentViewWhenFirstPage&&self.nowPage==1) {
//        [self showContent];
//    }else if (!self.showContentViewWhenFirstPage&&self.nowPage==1) {
//        [self showWaiting];
//    }
//    
//    NSMutableDictionary *mDic = [[NSMutableDictionary alloc]init];
//    [mDic setObject:[NSNumber numberWithInt:self.nowPage] forKey:PAGE_STR];
//
//    [mDic setObject:@15 forKey:PAGE_SIZE_STR];
//    if (_dicParam) {
//        [mDic addEntriesFromDictionary:_dicParam];
//    }
//    
////http://192.168.1.5/api/v1/terminal/check-version
////http://192.168.1.5/api/v1/terminal/check-version    
//    [GetUrl send:dateUrl methodType:_httpMethodType parameters:mDic success:^(AFHTTPRequestOperation *operation, id responseObject) {
//        if (self.delegate) {
//            [self.delegate onHttpSuccess:operation result:responseObject page:self.nowPage];
//        }
//        self.showContentViewWhenFirstPage = YES;
//
//        [self showContent];
//
//        if (_scroller) {
//            [_scroller.footer endRefreshing];
//            [_scroller.header endRefreshing];
//        }
//        
//    } failure:^(AFHTTPRequestOperation * _Nonnull operation, NSError * _Nonnull error) {
//        if (self.delegate) {
//            [self.delegate onHttpError:operation result:error page:self.nowPage];
//        }
//        if (self.showContentViewWhenFirstPage&&self.nowPage==1) {
//            [self showContent];
//        }else if (!self.showContentViewWhenFirstPage&&self.nowPage==1) {
//            
//            if (isSecondRequest) {
//                isSecondRequest = NO;
//                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//                [self showRetry];
//                });
//
//            }else{
//                isSecondRequest = YES;
//                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//                    [self beginGetDate:dateUrl and:0];
//                });
//
//            }
//            
//
//        }
//        
//        [_scroller.footer endRefreshing];
//        [_scroller.header endRefreshing];
//    }];
//    
//
//}

#pragma mark - MJRefresh
-(void)loadNewData{
    _nowPage = 1;
    [self getDate];
}
-(void)loadMoreData{
    _nowPage ++;
    [self getDate];
}
#pragma mark - IBActions
-(void)clickedRightControl:(id)sender{
    if (self.RightClick) {
        self.RightClick();
    }else
    NSLog(@"clickedRightControl");
}
-(void)clickedLeftControl:(id)sender{
    if (self.LeftClick) {
        self.LeftClick();
    }else
    NSLog(@"clickedLeftControl");
}

///**
// *  显示重试
// */
//-(void)showRetry{
//    self.viewRetryView.hidden = NO;
//    self.viewEmpty.hidden = YES;
//    self.viewContent.hidden = YES;
//    self.viewWaitingView.hidden = YES;
//}
///**
// *  显示空
// */
//-(void)showEmpty{
//    EmptyView *v  = [EmptyView  getView];
//    [v setUpLayoutWithViewController:self sameSizeView:self.viewContent];
//
//}
///**
// *  显示内容
// */
//-(void)showContent{
//    self.viewRetryView.hidden = YES;
//    self.viewEmpty.hidden = YES;
//    self.viewContent.hidden = NO;
//    self.viewWaitingView.hidden = YES;
//}
///**
// *  显示等待
// */
//-(void)showWaiting{
//    
//    self.viewRetryView.hidden = YES;
//    self.viewEmpty.hidden = YES;
//    self.viewContent.hidden = YES;
//    self.viewWaitingView.hidden = NO;
//    
//}
@end
